package client.comm.baoding.dialog

import android.app.Activity
import android.os.Process
import client.comm.commlib.base.BaseAppViewModel
import client.comm.commlib.dialog.UpdateVersionDialog
import client.comm.commlib.utils.Util

class UpDateDialog(context: Activity, viewModel: BaseAppViewModel, url: String) :
    MsgTitleDialog(context) {

    init {
        title = "有新版本更新?"
        okTxt = "更新"
        cancleTxt = "退出"
        setCanceledOnTouchOutside(true)
        setCancelable(false)

        cancle = {
            context.finish()
            Process.killProcess(context.application.applicationInfo.uid)
        }
        ok = {
            UpdateVersionDialog(context, viewModel).apply {
                done = {
                    Util.checkOreoAndInstallApk()
                    cancel()
                }
                error = {
                    cancel()
                    errorFun?.invoke()
                }
            }.startDown(url)
        }
    }

    var errorFun: (() -> Unit)? = null
}